﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMKJ.Classes;
using System.Text.RegularExpressions;

namespace LMKJ
{
    public partial class editresident : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dobInit();

                string nric = Request.QueryString["details"];

                Resident r = DatabaseManager.RetrieveResidentByNric(nric);

                if (r != null)
                {
                    nricLbl.Text = r.Nric;
                    nameTbx.Text = r.Name;
                    string[] dob = r.Dob.Split('-');
                    int day = Convert.ToInt32(dob[0]);
                    int month = Convert.ToInt32(dob[1]);
                    int year = Convert.ToInt32(dob[2]);
                    monthDdl.SelectedIndex = month;
                    dayInit();
                    dayDdl.SelectedIndex = day;
                    yearDdl.SelectedIndex = DateTime.Now.Year - year - 49;
                    int gender;
                    if (r.Gender.Equals("Male"))
                    {
                        gender = 0;
                    }
                    else
                    {
                        gender = 1;
                    }
                    genderRbl.SelectedIndex = gender;
                    addressTbx.Text = r.Address;
                    postalTbx.Text = r.Postal;
                    int ethnicity;
                    if (r.Ethnicity.Equals("Chinese"))
                    {
                        ethnicity = 0;
                    }
                    else if(r.Ethnicity.Equals("Malay"))
                    {
                        ethnicity = 1;
                    }
                    else if (r.Ethnicity.Equals("Indian"))
                    {
                        ethnicity = 2;
                    }
                    else
                    {
                        ethnicity = 3;
                    }
                    ethnicityDdl.SelectedIndex = ethnicity;
                    descriptionTbx.Text = r.Description;
                }
            }

        }

        protected void dobInit()
        {
            int thisYear = DateTime.Today.Year;

            for (int m = 1; m <= 12; m++)
            {
                monthDdl.Items.Add(m.ToString());
            }

            for (int y = thisYear - 51; y >= thisYear - 100; y--)
            {
                yearDdl.Items.Add(y.ToString());
            }
        }

        protected void dayInit()
        {
            int monthNo;
            int dayNo = 0;

            dayDdl.Enabled = true;
            yearDdl.Enabled = true;

            if (dayDdl.SelectedIndex > 1)
            {
                dayNo = Convert.ToInt32(dayDdl.SelectedItem.Text);
            }

            dayDdl.Items.Clear();
            dayDdl.Items.Add("Day");

            monthNo = monthDdl.SelectedIndex;

            for (int d = 1; d <= 31; d++)
            {
                dayDdl.Items.Add(d.ToString());
                if (monthNo == 2 && d == 29)
                {
                    break;
                }
                else if ((monthNo == 4 || monthNo == 6 || monthNo == 9 || monthNo == 11) && d == 30)
                {
                    break;
                }
            }

            if (dayDdl.Items.Count - 1 >= dayNo)
            {
                dayDdl.SelectedIndex = dayNo;
            }
        }

        protected void monthDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            int monthNo;
            int dayNo = 0;

            if (monthDdl.SelectedItem.Text != "Month")
            {
                dayDdl.Enabled = true;
                yearDdl.Enabled = true;

                if (dayDdl.SelectedIndex > 1)
                {
                    dayNo = Convert.ToInt32(dayDdl.SelectedItem.Text);
                }

                dayDdl.Items.Clear();
                dayDdl.Items.Add("Day");

                monthNo = monthDdl.SelectedIndex;

                for (int d = 1; d <= 31; d++)
                {
                    dayDdl.Items.Add(d.ToString());
                    if (monthNo == 2 && d == 29)
                    {
                        break;
                    }
                    else if ((monthNo == 4 || monthNo == 6 || monthNo == 9 || monthNo == 11) && d == 30)
                    {
                        break;
                    }
                }

                if (dayDdl.Items.Count - 1 >= dayNo)
                {
                    dayDdl.SelectedIndex = dayNo;
                }
            }
            else if (monthDdl.SelectedItem.Text == "Month")
            {
                dayDdl.SelectedIndex = 0;
                yearDdl.SelectedIndex = 0;
                dayDdl.Enabled = false;
                yearDdl.Enabled = false;
            }
        }

        protected void confirmBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Resident updatedResident = new Resident();
                updatedResident.Name = nameTbx.Text;
                updatedResident.Dob = yearDdl.SelectedItem.Text + "-" + monthDdl.SelectedItem.Text + "-" + dayDdl.SelectedItem.Text;
                updatedResident.Gender = genderRbl.SelectedItem.Text;
                updatedResident.Address = addressTbx.Text;
                updatedResident.Postal = postalTbx.Text;
                updatedResident.Ethnicity = ethnicityDdl.SelectedItem.Text;
                updatedResident.Nric = nricLbl.Text;

                int success = DatabaseManager.UpdateResident(updatedResident);

                if (success == 1)
                {
                    string jsPopup = "<script type='text/javascript'>" +
                            "alert('Successfully Updated Resident!');" +
                            "window.location.href = 'viewresidentdetails.aspx?details=" +
                            Request.QueryString["details"] +
                            "';" +
                            "</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
                }
            }
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("viewresidentdetails.aspx?details=" + Request.QueryString["details"]);
        }

        protected void deleteBtn_Click(object sender, EventArgs e)
        {
            confirmationLbl.Visible = true;
            confirmationBtn.Visible = true;
        }

        protected void fieldsValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (String.IsNullOrEmpty(nameTbx.Text.Trim())
                || String.IsNullOrEmpty(addressTbx.Text.Trim())
                || String.IsNullOrEmpty(postalTbx.Text.Trim())
                || monthDdl.SelectedIndex == 0 || dayDdl.SelectedIndex == 0 || yearDdl.SelectedIndex == 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void confirmationBtn_Click(object sender, EventArgs e)
        {
            int success = DatabaseManager.DeleteResident(Request.QueryString["details"]);

            if (success == 1)
            {
                string jsPopup = "<script type='text/javascript'>" +
                        "alert('Successfully Deleted Resident!');" +
                        "window.location.href = 'searchresidents.aspx';" +
                        "</script>";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
            }
        }

        protected void postalValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Regex NumberOnly = new Regex("\\d+$");

            if (NumberOnly.IsMatch(postalTbx.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}